package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserServerInfo;
import com.yanqu.road.entity.web.WebLoginParam;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserDaoImpl extends TempDataObjectDao<UserInfo> {

	public void getUserAbilityRank(int days, int titleId, List<UserInfo> userInfos, Map<Long, BigInteger> abilityMap) {
		String sql = "select a.*,b.ability from t_u_user a left join t_u_history_attribute b on a.userId=b.userId where a.lastLoginTime>=? or a.titleId>=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			long timeNeed = DateHelper.getTodayZeroTime() - DateHelper.DAY_SECONDS * days;
			int i = 1;
			call.setLong(i++, timeNeed);
			call.setInt(i++, titleId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				if (null == userInfo) {
					continue;
				}
				BigInteger ability = BigInteger.ZERO;
				String abilityStr = rs.getString("ability");
				if (!StringUtils.isNullOrEmpty(abilityStr)) {
					ability = new BigInteger(abilityStr);
				}
				userInfos.add(userInfo);
				abilityMap.put(userInfo.getUserId(), ability);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
	}

	public Map<Long, UserInfo> getBugUserInfoMap(String where) {
		String sql = "select * from t_u_user where " + where;
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Long, UserInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				dataMap.put(userInfo.getUserId(), userInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}


	public Map<Long, UserInfo> getUserInfoMap() {
		String sql = "select * from t_u_user";
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Long, UserInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				dataMap.put(userInfo.getUserId(), userInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public UserInfo getUserInfoByUserId(long userId) {
		String sql = "select * from t_u_user where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public UserInfo getUserInfoPreByUserId(long userId) {
		String sql = "select * from t_u_user_pre where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public UserInfo getUserInfoByUserId(long userId, String openId) {
		String sql = "select * from t_u_user where userId=? and openId = ?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setString(2, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public UserInfo register(WebLoginParam loginParam, long userId, long userAliasId, int hideVipAddress) {
		String sql = "INSERT INTO t_u_user(openId,registerTime,decoration,lastLoginTime,sex,nickName,platform,isAdmin,blackListEndTime,silver,patronsExp,earnSpeed,"
				+ "prestige,titleId,phonePlatform,userId,originalServerId,vipExp,childEarnSpeed,heartCount,lastHeartRecoverTime,baseEarnSpeed,houseLv,todayBeHateTimes,"
				+ "systemOpenValue,patronsCount,platformUserId,platformUserName,userAliasId,hideVipAndAddress,appId,childGameId,isInstructor,manorHelpCount,lastManorHelpRecoverTime,userMark,badge,ipLocation) " +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement call = null;
		UserInfo info;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			long nowTime = System.currentTimeMillis() / 1000;
			int i = 1;
			call.setString(i++, loginParam.getOpenId());
			call.setLong(i++, nowTime);
			call.setString(i++, "");
			call.setLong(i++, 0);
			call.setShort(i++, (short) 0);
			call.setString(i++, "");
			call.setString(i++, loginParam.getPlatform());
			call.setBoolean(i++, false);
			call.setLong(i++, 0);
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setInt(i++, 1);
			call.setInt(i++, loginParam.getPhonePlatform());
			call.setLong(i++, userId);
			call.setLong(i++, loginParam.getServerId());
			call.setLong(i++, 0L);
			call.setString(i++, "0");
			call.setInt(i++, 0);
			call.setLong(i++, nowTime);
			call.setString(i++, "0");
			call.setInt(i++, 1);
			call.setInt(i++, 0);
			call.setString(i++, "0");
			call.setInt(i++, 0);
			call.setString(i++, loginParam.getPlatformUserId());
			call.setString(i++, loginParam.getPlatformUserName());
			call.setLong(i++, userAliasId);
			call.setInt(i++, hideVipAddress);
			call.setString(i++, loginParam.getAppId());
			call.setString(i++, loginParam.getChildGameId());
			call.setBoolean(i++, false);
			call.setInt(i++, 0);
			call.setLong(i++, System.currentTimeMillis());
			call.setString(i++, "");
			call.setString(i++, "");
			call.setString(i++,"");
			if (call.executeUpdate() > 0) {
				info = new UserInfo();
				info.setUserId(userId);
				info.setPlatform(loginParam.getPlatform());
				info.setOpenId(loginParam.getOpenId());
				return info;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return null;
	}

	public UserInfo registerFromPre(UserInfo preUserInfo) {
		String sql = "INSERT INTO t_u_user(openId,registerTime,decoration,lastLoginTime,sex,nickName,platform,isAdmin,blackListEndTime,silver,patronsExp,earnSpeed,"
				+ "prestige,titleId,phonePlatform,userId,originalServerId,vipExp,childEarnSpeed,heartCount,lastHeartRecoverTime,baseEarnSpeed,houseLv,todayBeHateTimes,"
				+ "systemOpenValue,patronsCount,platformUserId,platformUserName,userAliasId,hideVipAndAddress,appId,childGameId,isInstructor,manorHelpCount,lastManorHelpRecoverTime,userMark,badge,ipLocation) " +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement call = null;
		UserInfo info;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			long nowTime = System.currentTimeMillis() / 1000;
			int i = 1;
			call.setString(i++, preUserInfo.getOpenId());
			call.setLong(i++, nowTime);
			call.setString(i++, preUserInfo.getDecoration());
			call.setLong(i++, 0);
			call.setShort(i++, preUserInfo.getSex());
			call.setString(i++, preUserInfo.getNickName());
			call.setString(i++, preUserInfo.getPlatform());
			call.setBoolean(i++, preUserInfo.isAdmin());
			call.setLong(i++, preUserInfo.getBlackListEndTime());
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setInt(i++, 1);
			call.setInt(i++, preUserInfo.getPhonePlatform());
			call.setLong(i++, preUserInfo.getUserId());
			call.setLong(i++, preUserInfo.getOriginalServerId());
			call.setLong(i++, 0L);
			call.setString(i++, "0");
			call.setInt(i++, 0);
			call.setLong(i++, nowTime);
			call.setString(i++, "0");
			call.setInt(i++, 1);
			call.setInt(i++, 0);
			call.setString(i++, "0");
			call.setInt(i++, 0);
			call.setString(i++, preUserInfo.getPlatformUserId());
			call.setString(i++, preUserInfo.getPlatformUserName());
			call.setLong(i++, preUserInfo.getUserAliasId());
			call.setInt(i++, preUserInfo.getHideVipAndAddress());
			call.setString(i++, preUserInfo.getAppId());
			call.setString(i++, preUserInfo.getChildGameId());
			call.setBoolean(i++, false);
			call.setInt(i++, 0);
			call.setLong(i++, System.currentTimeMillis());
			call.setString(i++, "");
			call.setString(i++, "");
			call.setString(i++,"");
			if (call.executeUpdate() > 0) {
				info = new UserInfo();
				info.setUserId(preUserInfo.getUserId());
				info.setPlatform(preUserInfo.getPlatform());
				info.setOpenId(preUserInfo.getOpenId());
				return info;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return null;
	}

	public UserInfo registerPre(WebLoginParam loginParam, long userId, long userAliasId, int hideVipAddress) {
		String sql = "INSERT INTO t_u_user_pre(openId,registerTime,decoration,lastLoginTime,sex,nickName,platform,isAdmin,blackListEndTime,silver,patronsExp,earnSpeed,"
				+ "prestige,titleId,phonePlatform,userId,originalServerId,vipExp,childEarnSpeed,heartCount,lastHeartRecoverTime,baseEarnSpeed,houseLv,todayBeHateTimes,"
				+ "systemOpenValue,patronsCount,platformUserId,platformUserName,userAliasId,hideVipAndAddress,appId,childGameId,isInstructor,manorHelpCount,lastManorHelpRecoverTime,userMark,badge,ipLocation) " +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement call = null;
		UserInfo info;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			long nowTime = System.currentTimeMillis() / 1000;
			int i = 1;
			call.setString(i++, loginParam.getOpenId());
			call.setLong(i++, nowTime);
			call.setString(i++, "");
			call.setLong(i++, 0);
			call.setShort(i++, (short) 0);
			call.setString(i++, "");
			call.setString(i++, loginParam.getPlatform());
			call.setBoolean(i++, false);
			call.setLong(i++, 0);
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setString(i++, "0");
			call.setInt(i++, 1);
			call.setInt(i++, loginParam.getPhonePlatform());
			call.setLong(i++, userId);
			call.setLong(i++, loginParam.getServerId());
			call.setLong(i++, 0L);
			call.setString(i++, "0");
			call.setInt(i++, 0);
			call.setLong(i++, nowTime);
			call.setString(i++, "0");
			call.setInt(i++, 1);
			call.setInt(i++, 0);
			call.setString(i++, "0");
			call.setInt(i++, 0);
			call.setString(i++, loginParam.getPlatformUserId());
			call.setString(i++, loginParam.getPlatformUserName());
			call.setLong(i++, userAliasId);
			call.setInt(i++, hideVipAddress);
			call.setString(i++, loginParam.getAppId());
			call.setString(i++, loginParam.getChildGameId());
			call.setBoolean(i++, false);
			call.setInt(i++, 0);
			call.setLong(i++, System.currentTimeMillis());
			call.setString(i++, "");
			call.setString(i++, "");
			call.setString(i++,"");
			if (call.executeUpdate() > 0) {
				info = new UserInfo();
				info.setUserId(userId);
				info.setPlatform(loginParam.getPlatform());
				info.setOpenId(loginParam.getOpenId());
				return info;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return null;
	}

	public boolean registerPreServer(String opneId, long userId,long serverId) {
		String sql = "INSERT INTO t_s_pre_role(openId,userId,serverId,`time`) " +
				" VALUES(?,?,?,?)";
		PreparedStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, opneId);
			call.setLong(i++, userId);
			call.setLong(i++, serverId);
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			if (call.executeUpdate() > 0) {
				return true;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean updatePreServer(String nickName,long userId) {
		String sql = "UPDATE t_s_pre_role SET nickName=? WHERE userId=?";
		PreparedStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, nickName);
			call.setLong(i++, userId);
			if (call.executeUpdate() > 0) {
				return true;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean isSameNickName(String nickName) {
		String sql = "select * from t_u_user where nickName=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, nickName);
			rs = call.executeQuery();
			while (rs.next()) {
				String selectNickName = rs.getString("nickName");
				if(selectNickName.equals(nickName)){
					return true;
				}else{
					return false;
				}
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return false;
	}

	public boolean updateNickName(long userId, String nickName) {
		String sql = "UPDATE t_u_user SET nickName=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, nickName));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateUnionUid(long userId, String unionUid) {
		String sql = "UPDATE t_u_user SET unionUid=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, unionUid));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateUserPlatformUserId(long userId, String platformUserId, String platformUserName) {
		String sql = "UPDATE t_u_user SET platformUserId=?,platformUserName=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.VARCHAR, platformUserId));
			para.put(i++, new DbParameter(Types.VARCHAR, platformUserName));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateHeartCountAndEarnSpeed(long userId, int heartCount, BigInteger earnSpeed) {
		String sql = "UPDATE t_u_user SET heartCount=?,earnSpeed=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, heartCount));
			para.put(i++, new DbParameter(Types.VARCHAR, earnSpeed.toString()));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateUserRoleInfo(long userId, short sex, String nickName, String decoration) {
		String sql = "UPDATE t_u_user SET sex=?,nickName=?,decoration=?,createRoleTime=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, sex));
			para.put(i++, new DbParameter(Types.VARCHAR, nickName));
			para.put(i++, new DbParameter(Types.VARCHAR, decoration));
			para.put(i++, new DbParameter(Types.BIGINT, System.currentTimeMillis() / 1000));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updatePreUserRoleInfo(long userId, short sex, String nickName, String decoration) {
		String sql = "UPDATE t_u_user_pre SET sex=?,nickName=?,decoration=?,createRoleTime=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, sex));
			para.put(i++, new DbParameter(Types.VARCHAR, nickName));
			para.put(i++, new DbParameter(Types.VARCHAR, decoration));
			para.put(i++, new DbParameter(Types.BIGINT, System.currentTimeMillis() / 1000));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateLastHeartRecoverTimeAndEarnSpeed(long userId, int heartCount, long lastHeartRecoverTime, BigInteger earnSpeed) {
		String sql = "UPDATE t_u_user SET heartCount=?,lastHeartRecoverTime=?,earnSpeed=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, heartCount));
			para.put(i++, new DbParameter(Types.BIGINT, lastHeartRecoverTime));
			para.put(i++, new DbParameter(Types.VARCHAR, earnSpeed.toString()));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateHeartCountAndEarnSpeedAndHateTimes(long userId, int heartCount, BigInteger earnSpeed, int beHateTimes) {
		String sql = "UPDATE t_u_user SET heartCount=?,earnSpeed=?,todayBeHateTimes=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, heartCount));
			para.put(i++, new DbParameter(Types.VARCHAR, earnSpeed.toString()));
			para.put(i++, new DbParameter(Types.INTEGER, beHateTimes));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateLastHeartRecoverTimeAndEarnSpeedAndHateTimes(long userId, int heartCount, long lastHeartRecoverTime, BigInteger earnSpeed, int beHateTimes) {
		String sql = "UPDATE t_u_user SET heartCount=?,lastHeartRecoverTime=?,earnSpeed=?,todayBeHateTimes=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, heartCount));
			para.put(i++, new DbParameter(Types.BIGINT, lastHeartRecoverTime));
			para.put(i++, new DbParameter(Types.VARCHAR, earnSpeed.toString()));
			para.put(i++, new DbParameter(Types.INTEGER, beHateTimes));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateChildEarnSpeed(long userId, BigInteger childEarnSpeed) {
		String sql = "UPDATE t_u_user SET childEarnSpeed=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, childEarnSpeed.toString()));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateForbid(long userId, long blackListEndTime) {
		String sql = "UPDATE t_u_user SET blackListEndTime=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.BIGINT, blackListEndTime));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateForbidSpeak(long userId, long forbiddenSpeakEndTime) {
		String sql = "UPDATE t_u_user SET forbiddenSpeakEndTime=? WHERE userId=?";
		try{
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.BIGINT, forbiddenSpeakEndTime));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateAdmin(long userId, boolean isAdmin) {
		String sql = "UPDATE t_u_user SET isAdmin=? WHERE userId=?";
		try{
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.BOOLEAN, isAdmin));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public long getMaxUserId() {
		String sql = "select ifnull(max(userId),0) as maxId from t_u_user";
		ResultSet rs = null;
		CallableStatement call = null;
		long maxId = 0;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				maxId = rs.getLong("maxId");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return maxId;
	}

	public List<Long> getUserPreServerList(String openId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<Long> serverIdList = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall("SELECT * FROM `t_s_pre_role` WHERE openId = ? and nickName != ''");
			call.setString(1, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				serverIdList.add(rs.getLong("serverId"));
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return serverIdList;
	}

	public long getMaxUserIdPre() {
		String sql = "select ifnull(max(userId),0) as maxId from t_u_user_pre";
		ResultSet rs = null;
		CallableStatement call = null;
		long maxId = 0;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				maxId = rs.getLong("maxId");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return maxId;
	}

	//根据用户openid获取用户的所有区服
    public List<UserServerInfo> getUserServerList(String openId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<UserServerInfo> userServerList = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall("SELECT * FROM `t_s_user_list` su" +
					" LEFT JOIN t_s_server_list ss on ss.serverId = su.serverId where su.openId=? ");
			call.setString(1, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserServerInfo info = new UserServerInfo();
				info.setServerId(rs.getInt("serverId"));
				info.setUserId(rs.getLong("userId"));
				info.setChannelId(rs.getInt("channelId"));
				info.setServerName(rs.getString("serverName"));
				userServerList.add(info);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return userServerList;
    }


	public UserInfo getUserInfoByOpenIdAndServerId(String openId, long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user where openId=? and originalServerId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			call.setLong(2, serverId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public List<UserInfo> getUserInfoList(String openId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<UserInfo> dataList = new ArrayList<>();
		String sql = "select * from t_u_user where openId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo data = parse(rs);
				dataList.add(data);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public UserInfo getUserInfoByUserAliasId(long userAliasId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user where userAliasId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userAliasId);
			rs = call.executeQuery();
			if (rs.next()) {
				return parse(rs);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public boolean updateBugGoldSpeed(UserInfo userInfo) {
		String sql = "UPDATE t_u_user SET earnSpeed=?,baseEarnSpeed=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, userInfo.getEarnSpeed().toString()));
			para.put(i++, new DbParameter(Types.NVARCHAR, userInfo.getBaseEarnSpeed().toString()));
			para.put(i++, new DbParameter(Types.BIGINT, userInfo.getUserId()));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateUserMark(long userId, String userMark) {
		String sql = "UPDATE t_u_user SET userMark=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, userMark));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		}
	}

	public List<UserInfo> getUserInfosByOpenIdAndServerId(String openId, long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user where openId=? and originalServerId=?";
		List<UserInfo> userInfoList = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			call.setLong(2, serverId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				userInfoList.add(userInfo);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return userInfoList;
	}

	public UserInfo getPreUserInfosByOpenIdAndServerId(String openId, long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user_pre where openId=? and originalServerId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			call.setLong(2, serverId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public UserInfo getPreUserInfosByOpenIdFull(String openId, long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user_pre where openId=? and originalServerId=? AND nickName != ''";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			call.setLong(2, serverId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public int getUserCountByOpenId(String openId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select count(1) as allCount from t_s_pre_role where openId=?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			rs = call.executeQuery();
			while (rs.next()) {
				return rs.getInt("allCount");
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return 0;
	}

	public UserInfo getUserInfoByOpenIdAndPhonePlatform(String openId, int phonePlatform, long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user where openId=? and phonePlatform=? and originalServerId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, openId);
			call.setInt(2, phonePlatform);
			call.setLong(3, serverId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				return userInfo;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserInfo data) {
		return false;
	}

	@Override
	protected boolean updateData(UserInfo data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_user SET openId=?,originalServerId=?,language=?,sex=?,nickName=?,decoration=?,titleId=?,unionUid=?,registerTime=?,lastLoginTime=?,"
				+ "platform=?,world=?,continent=?,country=?,province=?,city=?,isAdmin=?,blackListEndTime=?,silver=?,patronsExp=?,earnSpeed=?,prestige=?,forbiddenSpeakEndTime=?,phonePlatform=?,"
				+ "vipExp=?,childEarnSpeed=?,heartCount=?,lastHeartRecoverTime=?,baseEarnSpeed=?,houseLv=?,todayBeHateTimes=?,systemOpenValue=?,patronsCount=?,hideVipAndAddress=?,houseCreditLv=?,isInstructor=?,"
				+ "manorHelpCount=?,lastManorHelpRecoverTime=?,totalIntimacy=?,totalAbility=?,badge=?,ipLocation = ?,bankSkinId = ?"
				+ " WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getOpenId());
			call.setLong(i++, data.getOriginalServerId());
			call.setString(i++, data.getLanguage());
			call.setShort(i++, data.getSex());
			call.setString(i++, data.getNickName());
			call.setString(i++, data.getDecoration());
			call.setInt(i++, data.getTitleId());
			call.setString(i++, data.getUnionUid());
			call.setLong(i++, data.getRegisterTime());
			call.setLong(i++, data.getLastLoginTime());
			call.setString(i++, data.getPlatform());
			call.setString(i++, data.getWorld());
			call.setString(i++, data.getContinent());
			call.setString(i++, data.getCountry());
			call.setString(i++, data.getProvince());
			call.setString(i++, data.getCity());
			call.setBoolean(i++, data.isAdmin());
			call.setLong(i++, data.getBlackListEndTime());
			call.setString(i++, data.getSilver().toString());
			call.setString(i++, data.getPatronsExp().toString());
			call.setString(i++, data.getEarnSpeed().toString());
			call.setString(i++, data.getPrestige().toString());
			call.setLong(i++, data.getForbiddenSpeakEndTime());
			call.setInt(i++, data.getPhonePlatform());
			call.setLong(i++, data.getVipExp());
			call.setString(i++, data.getChildEarnSpeed().toString());
			call.setInt(i++, data.getHeartCount());
			call.setLong(i++, data.getLastHeartRecoverTime());
			call.setString(i++, data.getBaseEarnSpeed().toString());
			call.setInt(i++, data.getHouseLv());
			call.setInt(i++, data.getTodayBeHateTimes());
			call.setString(i++, data.getSystemOpenValue().toString());
			call.setInt(i++, data.getPatronsCount());
			call.setInt(i++, data.getHideVipAndAddress());
			call.setInt(i++, data.getHouseCreditLv());
			call.setBoolean(i++, data.isInstructor());
			call.setInt(i++, data.getManorHelpCount());
			call.setLong(i++, data.getLastManorHelpRecoverTime());
			call.setString(i++, data.getTotalIntimacy().toString());
			call.setString(i++, data.getTotalAbility().toString());
			call.setString(i++, data.getBadge());
			call.setString(i++, data.getIpLocation());
			call.setInt(i++, data.getBankSkinId());

			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserInfo parseData(ResultSet rs) throws SQLException {
		UserInfo data = new UserInfo();
		data.setUserId(rs.getLong("userId"));
		data.setOpenId(rs.getString("openId"));
		data.setOriginalServerId(rs.getInt("originalServerId"));
		data.setLanguage(rs.getString("language"));
		data.setSex(rs.getShort("sex"));
		data.setNickName(rs.getString("nickName") == null ? "" : rs.getString("nickName"));
		data.setDecoration(rs.getString("decoration"));
		data.setTitleId(rs.getInt("titleId"));
		data.setUnionUid(rs.getString("unionUid"));
		data.setRegisterTime(rs.getLong("registerTime"));
		data.setLastLoginTime(rs.getLong("lastLoginTime"));
		data.setPlatform(rs.getString("platform"));
		data.setWorld(rs.getString("world"));
		data.setContinent(rs.getString("continent"));
		data.setCountry(rs.getString("country"));
		data.setProvince(rs.getString("province"));
		data.setCity(rs.getString("city"));
		data.setAdmin(rs.getBoolean("isAdmin"));
		data.setBlackListEndTime(rs.getLong("blackListEndTime"));
		data.setForbiddenSpeakEndTime(rs.getLong("forbiddenSpeakEndTime"));
		data.setPhonePlatform(rs.getInt("phonePlatform"));
		data.setVipExp(rs.getLong("vipExp"));
		data.setPlatformUserId(rs.getString("platformUserId"));
		data.setHideVipAndAddress(rs.getInt("hideVipAndAddress"));
		data.setAppId(rs.getString("appId"));
		data.setChildGameId(rs.getString("childGameId"));

		String value = rs.getString("silver");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setSilver(new BigInteger(value));
		}

		value = rs.getString("patronsExp");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setPatronsExp(new BigInteger(value));
		}

		value = rs.getString("prestige");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setPrestige(new BigInteger(value));
		}

		value = rs.getString("earnSpeed");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setEarnSpeed(new BigInteger(value));
		}

		value = rs.getString("childEarnSpeed");
		if(!StringUtils.isNullOrEmpty(value)){
			data.setChildEarnSpeed(new BigInteger(value));
		}
		data.setHeartCount(rs.getInt("heartCount"));
		data.setLastHeartRecoverTime(rs.getLong("lastHeartRecoverTime"));
		value = rs.getString("baseEarnSpeed");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setBaseEarnSpeed(new BigInteger(value));
		}
		data.setHouseLv(rs.getInt("houseLv"));
		data.setTodayBeHateTimes(rs.getInt("todayBeHateTimes"));
		value = rs.getString("systemOpenValue");
		if(!StringUtils.isNullOrEmpty(value)){
			data.setSystemOpenValue(new BigInteger(value));
		}
		data.setPatronsCount(rs.getInt("patronsCount"));
		data.setUserAliasId(rs.getLong("userAliasId"));
		data.setHouseCreditLv(rs.getInt("houseCreditLv"));
		data.setInstructor(rs.getBoolean("isInstructor"));
		data.setManorHelpCount(rs.getInt("manorHelpCount"));
		data.setLastManorHelpRecoverTime(rs.getLong("lastManorHelpRecoverTime"));
		data.setUserMark(rs.getString("userMark"));
		String totalAbility = rs.getString("totalAbility");
		if (!StringUtils.isNullOrEmpty(totalAbility)) {
			data.setTotalAbility(new BigInteger(totalAbility));
		}
		String totalIntimacy = rs.getString("totalIntimacy");
		if (!StringUtils.isNullOrEmpty(totalIntimacy)) {
			data.setTotalIntimacy(new BigInteger(totalIntimacy));
		}
		data.setBadge(rs.getString("badge"));
		data.setIpLocation(rs.getString("ipLocation"));
		data.setCreateRoleTime(rs.getLong("createRoleTime"));
		data.setBankSkinId(rs.getInt("bankSkinId"));
		return data;
	}

	public boolean checkSqlConnectionStatus() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select userId from t_u_user limit 1";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			return false;
		} finally {
			getDb().closeConnection(call, rs);
		}
		return true;
	}

	public boolean updateLastManorHelpRecoverTimeAndCount(long helpUserId, long recoverTime, int manorHelpCount) {
		String sql = "UPDATE t_u_user SET manorHelpCount=?,lastManorHelpRecoverTime=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, manorHelpCount));
			para.put(i++, new DbParameter(Types.BIGINT, recoverTime));
			para.put(i++, new DbParameter(Types.BIGINT, helpUserId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public boolean updateManorHelpCount(long helpUserId, int manorHelpCount) {
		String sql = "UPDATE t_u_user SET manorHelpCount=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.INTEGER, manorHelpCount));
			para.put(i++, new DbParameter(Types.BIGINT, helpUserId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	public UserInfo getUserInfoByAliasId(long userAliasId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user where userAliasId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userAliasId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public List<UserInfo> getUserInfoByPlatformUserId(String platformUserId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_user where platformUserId=? ";
		List<UserInfo> list = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, platformUserId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				list.add(userInfo);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	public List<Long> getUserIdListByLastLoginTimeAndHasUnion(long lastLoginTime) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select userId from t_u_user where lastLoginTime > ? AND unionUid != ''";
		List<Long> list = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, lastLoginTime);
			rs = call.executeQuery();
			while (rs.next()) {
				long userId = rs.getLong("userId");
				list.add(userId);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	public boolean destroyRole(long userId, String destroyRoleOpenId) {
		String sql = "UPDATE t_u_user SET openId=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			Map<Integer, DbParameter> para = new HashMap<>();
			int i = 1;
			para.put(i++, new DbParameter(Types.NVARCHAR, destroyRoleOpenId));
			para.put(i++, new DbParameter(Types.BIGINT, userId));
			return getDb().executeNoneQuery(sql, para) > -1;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
		return false;
	}

	/**
	* 描述：获取创角数
	* 作者：zrq
	* 时间：2023/11/17
	* 参数：

	* 返回值：
	**/
	public long getRoleCreateCount() {
		String sql = "SELECT COUNT(userId) as count FROM `t_u_user` WHERE `nickName` <> '' AND `nickName` IS NOT NULL";
		ResultSet rs = null;
		CallableStatement call = null;
		long count = 0;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				count = rs.getLong("count");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return count;
	}

	public long getRoleCreateCount2(int level) {
		String sql = "SELECT COUNT(userId) as count FROM `t_u_user` WHERE `nickName` <> '' AND `nickName` IS NOT NULL AND `titleId` >= ?";
		ResultSet rs = null;
		CallableStatement call = null;
		long count = 0;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			int i = 1;
			call.setInt(i++, level);
			rs = call.executeQuery();
			while (rs.next()) {
				count = rs.getLong("count");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return count;
	}

	public Map<Long, UserInfo> getUserInfoMapWithTitleId(int titleId) {
		String sql = "select * from t_u_user where titleId >= ? order by titleId desc ";
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Long, UserInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			int i = 1;
			call.setInt(i++, titleId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				dataMap.put(userInfo.getUserId(), userInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public int getRegisterUserCount() {
		String sql = "select count(*) as rCount from t_u_user";
		ResultSet rs = null;
		CallableStatement call = null;
		int count = 0;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				count = rs.getInt("rCount");
				return count;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return count;
	}

	public Map<Long, UserInfo> getCreateRoleUserInfoMap() {
		String sql = "select * from t_u_user WHERE `nickName` <> '' AND `nickName` IS NOT NULL";
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Long, UserInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UserInfo userInfo = parse(rs);
				dataMap.put(userInfo.getUserId(), userInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
